﻿<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" type="text/css" href="../../static/h-ui/css/H-ui.min.css" />
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/css/H-ui.admin.css" />
    <link rel="stylesheet" type="text/css" href="../../lib/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="../../static/h-ui.admin/css/style.css" />
    <title>服务授权</title>
    <style type="text/css">
        table {
            border-collapse: separate;
            background-color: #a5c8e2;
            border-spacing: 1px;
            width: 100%;
        }

        th {
            background-color: #f5fafe;
            font-weight: bold;
            color: #333;
            text-align: center;
            line-height: 42px;
            height: 42px;
        }

        td {
            background-color: white;
            text-align: center;
            height: 38px;
            line-height: 38px;
        }
        label{
            cursor: pointer;
        }
        input[type=checkbox]{
            cursor: pointer;
        }
    </style>
</head>

<body>

    <div class="page-container">
        <div class="cl pd-5 bg-1 bk-gray mt-20">
            <a class="btn btn-success radius r" style="line-height:1.6em;margin-top:3px"
                href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>

        </div>
        <script type="text/html" id="templatemodel">
            <span class="item"><label><input  type="checkbox" [enablestr] datatype="model" datastr="[code]">[name]([code])</label></span>
        </script>
        <script type="text/html" id="templatetr">
            <tr dataid="[service:id]">
                <td class="fleft">[project:name]([project:code])</td>
                <td class="fleft">[service:name]([service:code])</td>
                <td><input type="checkbox" class="linestate" datatype="line" [enablestr]></td>
                <td class="fleft ulmodel">[modelstr]</td>
            </tr>
        </script>
        <div class="mt-20">
            <table class="table">
                <thead>
                    <tr class="text-c">
                        <th>项目</th>
                        <th>服务</th>
                        <th>状态</th>
                        <th>环境</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>

    <!--_footer 作为公共模版分离出去-->
    <script type="text/javascript" src="../../lib/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="../../lib/layer/2.4/layer.js"></script>
    <script type="text/javascript" src="../../static/h-ui/js/H-ui.min.js"></script>
    <script type="text/javascript" src="../../static/h-ui.admin/js/H-ui.admin.js"></script>
    <!--/_footer 作为公共模版分离出去-->
    <!--请在下方写此页面业务相关的脚本-->
    <!-- <script type="text/javascript" src="../../lib/My97DatePicker/4.8/WdatePicker.js"></script> -->
    <script src="../../lib/api.js"></script>

    <script>
        var pageobject = null;
        var isselect = false;
        function _showpagedata(datas) {
            $("table").find("tbody").empty();
            var hstr = "";
            for (var k = 0; k < datas.length; k++) {
                var item = datas[k];
                var htmlStr = '<tr class="text-c">';
                htmlStr += '<td>' + item.id + '</td>';
                htmlStr += '<td>' + item.userName + '</td>';
                htmlStr += '<td>' + item.createDate + '</td>';
                htmlStr += '<td>' + item.email + '</td>';
                htmlStr += '<td>' + item.desc + '</td>';
                htmlStr += '<td>' + item.isEnable + '</td>';
                htmlStr += '<td>' + item.grade + '</td>';

                htmlStr += '<td><a title="编辑" href="javascript:;" onclick="_showedit(\'编辑\',\'view.html\',\'' + item.id + '\',\'80%\',\'80%\')" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6df;</i></a></td>'

                htmlStr += '</tr>';
                hstr += htmlStr;
            }
            $("table").find("tbody").append(hstr);
        }

        /*
        *选择一个对象 
        */
        function _functionSelect(elc) {
            var dataid = parseInt($(elc).attr("dataid"));
            var info = { id: dataid, name: $(elc).attr("datastr") };
            window.parent.postMessage({ type: 'appinfo', data: info });

            //关闭自己
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);  // 关闭layer
        }

        var groupid = 0;

        var _id = 0;//user.id
        $(window).ready(function () {

            var idstr = _apigetquery('id');
            if (idstr != null) {
                _id = parseInt(idstr);
            }

            _apiget('/api/spider/user/ReadUserAuth?userid=' + _id, false, function (code, obj) {
                if (code == 200) {
                    var templatemodel = $("#templatemodel").html();
                    var templatetr = $("#templatetr").html();
                    var ahtml = '';
                    for (var k = 0; k < obj.length; k++) {
                        var item = obj[k];
                        if (item.isEnable) {
                            item.enablestr = 'checked';
                        } else {
                            item.enablestr = '';
                        }
                        if (item.models.length > 0) {
                            var modelstr = '';
                            for (var i = 0; i < item.models.length; i++) {
                                var one = item.models[i];
                                if (one.isEnable) {
                                    one.enablestr = 'checked'
                                } else {
                                    one.enablestr = '';
                                }
                                modelstr += _buildtemplate(templatemodel, one);
                            }
                            item.modelstr = modelstr;

                        } else {
                            item.modelstr = '';
                        }
                        ahtml += _buildtemplate(templatetr, item);
                    }
                    $(".table").find("tbody").html(ahtml);
                }
            });

            $(".table").on("change", "[type=checkbox]", function (e) {
                var checkval = $(this).prop("checked");
                var dtype = $(this).attr('datatype');
                var linetr = $(this).parents('tr');

                var eventelc=$(this);

                var info = {
                    serviceid: $(linetr).attr('dataid'),
                    userid: _id,
                    isenable: $(linetr).find('.linestate').prop('checked'),
                    codes: new Array()
                }
                //组合计算codes
                var items = $(linetr).find('.item').find('input[type=checkbox]');
                if (items.length > 0) {
                    for (var i = 0; i < items.length; i++) {
                        var elc = $(items[i]);
                        if (elc.prop('checked')) {
                            info.codes.push(elc.attr('datastr'));
                        }
                    }
                }

                //推送出去
                // console.log(info);
                _apipost('/api/spider/user/UpdateUserAuth',false,JSON.stringify(info),function(code,obj){
                    if(code==200){
                        // layer.msg();
                    }else{
                        eventelc.prop('checked',!checkval);
                    }
                })

            })

        });

    </script>


</body>

</html>